package org.jeecg.modules.xxgx.fjys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.xxgx.fjys.entity.CsXxCjmb;
import org.jeecg.modules.xxgx.fjys.entity.CsXxCjmbFjys;
import org.jeecg.modules.xxgx.fjys.service.ICsXxCjmbFjysService;
import org.jeecg.modules.xxgx.fjys.service.ICsXxCjmbService;
import org.jeecg.modules.xxgx.fjys.vo.CsXxCjmbPage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;


 /**
 * @Description: 场景模板
 * @Author: jeecg-boot
 * @Date:   2023-10-09
 * @Version: V1.0
 */
@Api(tags="场景模板")
@RestController
@RequestMapping("/xxgx/fjys/csXxCjmb")
@Slf4j
public class CsXxCjmbController {
	@Autowired
	private ICsXxCjmbService csXxCjmbService;
	@Autowired
	private ICsXxCjmbFjysService csXxCjmbFjysService;

	/**
	 * 分页列表查询
	 *
	 * @param csXxCjmb
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "场景模板-分页列表查询")
	@ApiOperation(value="场景模板-分页列表查询", notes="场景模板-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<CsXxCjmb>> queryPageList(CsXxCjmb csXxCjmb,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<CsXxCjmb> queryWrapper = QueryGenerator.initQueryWrapper(csXxCjmb, req.getParameterMap());
		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		Page<CsXxCjmb> page = new Page<CsXxCjmb>(pageNo, pageSize);
		queryWrapper.eq("company_id", user.getCompanyId());
		IPage<CsXxCjmb> pageList = csXxCjmbService.page(page, queryWrapper);
		csXxCjmbService.handleCjmb(pageList.getRecords());
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param csXxCjmbPage
	 * @return
	 */
	@AutoLog(value = "场景模板-添加")
	@ApiOperation(value="场景模板-添加", notes="场景模板-添加")
    @RequiresPermissions("xxgx.fjys:cs_xx_cjmb:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody CsXxCjmbPage csXxCjmbPage) {
		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		QueryWrapper<CsXxCjmb> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("company_id",user.getCompanyId());
		queryWrapper.eq("cjmbmc",csXxCjmbPage.getCjmbmc());
		int count = (int) csXxCjmbService.count(queryWrapper);
		if(count > 0){
			throw new JeecgBootException(csXxCjmbPage.getCjmbmc() + " 已存在！");
		}
		CsXxCjmb csXxCjmb = new CsXxCjmb();
		BeanUtils.copyProperties(csXxCjmbPage, csXxCjmb);
		csXxCjmb.setCompanyId(user.getCompanyId());
		csXxCjmbService.saveMain(csXxCjmb, csXxCjmbPage.getCsXxFjysList());
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param csXxCjmbPage
	 * @return
	 */
	@AutoLog(value = "场景模板-编辑")
	@ApiOperation(value="场景模板-编辑", notes="场景模板-编辑")
    @RequiresPermissions("xxgx.fjys:cs_xx_cjmb:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody CsXxCjmbPage csXxCjmbPage) {
		CsXxCjmb csXxCjmb = new CsXxCjmb();
		BeanUtils.copyProperties(csXxCjmbPage, csXxCjmb);
		CsXxCjmb csXxCjmbEntity = csXxCjmbService.getById(csXxCjmb.getId());
		if(csXxCjmbEntity==null) {
			return Result.error("未找到对应数据");
		}
		csXxCjmbService.updateMain(csXxCjmb, csXxCjmbPage.getCsXxFjysList());
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "场景模板-通过id删除")
	@ApiOperation(value="场景模板-通过id删除", notes="场景模板-通过id删除")
    @RequiresPermissions("xxgx.fjys:cs_xx_cjmb:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		csXxCjmbService.delMain(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "场景模板-批量删除")
	@ApiOperation(value="场景模板-批量删除", notes="场景模板-批量删除")
    @RequiresPermissions("xxgx.fjys:cs_xx_cjmb:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.csXxCjmbService.delBatchMain(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功！");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "场景模板-通过id查询")
	@ApiOperation(value="场景模板-通过id查询", notes="场景模板-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<CsXxCjmb> queryById(@RequestParam(name="id",required=true) String id) {
		CsXxCjmb csXxCjmb = csXxCjmbService.getById(id);
		if(csXxCjmb==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(csXxCjmb);

	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "场景模板附加要素明细通过主表ID查询")
	@ApiOperation(value="场景模板附加要素明细主表ID查询", notes="场景模板附加要素明细-通主表ID查询")
	@GetMapping(value = "/queryCsXxCjmbFjysByMainId")
	public Result<List<CsXxCjmbFjys>> queryCsXxCjmbFjysListByMainId(@RequestParam(name="id",required=true) String id) {
		List<CsXxCjmbFjys> csXxCjmbFjysList = csXxCjmbFjysService.selectByMainId(id);
		return Result.OK(csXxCjmbFjysList);
	}

}
